package com.capgemini.cn.authority.data.entity;

import com.capgemini.cn.authority.data.core.DefaultIdentityEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.List;

/**
 * <p>资源分组表</p>
 * Created at 2017/8/23
 *
 * @author Andriy
 */
@Entity
@Table(name = "cg_asserts_group")
@NoArgsConstructor
@AllArgsConstructor
public class AssertsGroup extends DefaultIdentityEntity {

    /**
     * 资源组别名称
     */
    @Column(name = "asserts_group_name", length = 50, nullable = false)
    @Getter
    @Setter
    private String assertsGroupName;

    /**
     * 资源组成员集合
     */
    @OneToMany(targetEntity = Asserts.class)
    @JoinColumn(name = "assert_group_id")
    @Getter
    @Setter
    private List<Asserts> asserts;

    /**
     * <p>资源组与权限的多对多关系</p>
     * <p>关系维护者为{@link AssertsGroup}对象，即对应的资源组被删除时相应关联的权限关系也会一并被解除，反之移除对应的权限数据之前需先手动解除资源组与权限数据的关系</p>
     */
    @ManyToMany(targetEntity = Authorities.class, cascade = {
            CascadeType.DETACH
    })
    @JoinTable(name = "cg_asserts_group_authorities", joinColumns = {
            @JoinColumn(name = "assert_group_id", referencedColumnName = "id", nullable = false, updatable = false)
    }, inverseJoinColumns = {
            @JoinColumn(name = "authority_id", referencedColumnName = "id", nullable = false, updatable = false)
    })
    @Getter
    @Setter
    private List<Authorities> authorities;
}
